package kr.hyunist.androidcomplete02saeildamil.ch25;

import kr.hyunist.androidcomplete02saeildamil.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;

public class PrefTest extends Activity {
	EditText textName;
	EditText textStNum;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.preftest);
		
		textName = (EditText) findViewById(R.id.preftestet1);
		textStNum = (EditText) findViewById(R.id.preftestet2);
		
		//PrefTest.xml파일을 읽기 쓰기가 가능하 ㄴ상태로 생성해서 열음
		SharedPreferences pref = getSharedPreferences("PrefTest", 0);
		String name = pref.getString("name", "이름없음");
		int num = pref.getInt("num", 20101234);
		
		textName.setText(name);
		textStNum.setText("" + num);
	}
	
	//back키를 눌러서 onPause상태로 가면 상태가 xml에 저장
	@Override
	protected void onPause() {
		super.onPause();
		
		SharedPreferences pref = getSharedPreferences("PrefTest", 0);
		SharedPreferences.Editor edit = pref.edit();
		
		String name = textName.getText().toString();
		int num = 0;
		try {
			num = Integer.parseInt(textStNum.getText().toString());
		} catch(Exception e) {
			textStNum.setText("numberformat exception이여!");
		}
		
		
		edit.putString("name", name);
		edit.putInt("num", num);
		
		edit.commit();
	}
}
